#pragma once
// IBUILDER BY ZERO123
#include "MFSI.hpp"
#define BUILDER_CALL __cdecl

// 导出声明
#ifdef _MSC_VER
	#define QCUSTOM_API extern "C" __declspec(dllexport)
	#elif defined(__GNUC__)
	#define QCUSTOM_API extern "C" __attribute__((visibility("default")))
	#else
	#define QCUSTOM_API extern "C"
#endif

#ifdef __cplusplus
extern "C" {
#endif
	typedef void (BUILDER_CALL* REPL_VAR_REFS_CALL_BACK)(void*, const char*);
	struct IMCBuilder
	{
		void* _this;
		void (BUILDER_CALL* getEnv)(void*, const char*, char*);
		void (BUILDER_CALL* setEnv)(void*, const char*, const char*);
		void (BUILDER_CALL* logInfo)(void*, const char*);
		void (BUILDER_CALL* logSuccess)(void*, const char*);
		void (BUILDER_CALL* logError)(void*, const char*);
		void (BUILDER_CALL* logErrorThrow)(void*, const char*);
		void (BUILDER_CALL* logWarning)(void*, const char*);
		void (BUILDER_CALL* replVarRefs)(void*, const char*, void*, REPL_VAR_REFS_CALL_BACK);
	};

	typedef struct IMemoryPath (BUILDER_CALL* _GetModFSFunc)(void*);
	struct IMCBuildContext
	{
		void* _this;
		IMemoryPath* buildPath;
		IMemoryPath* behFS;
		IMemoryPath* resFS;
		_GetModFSFunc modFS;
	};
#ifdef __cplusplus
}

// 适用于CPP的视图包装
#include <iostream>
#include <vector>
namespace QAddon
{
	class MCBuilderView
	{
	private:
		const IMCBuilder* IBuilder;
	public:
		MCBuilderView(const IMCBuilder* builder): IBuilder(builder) {}

		std::string getEnv(const std::string& envName) const
		{
			char env[512]{};
			IBuilder->getEnv(IBuilder->_this, envName.c_str(), env);
			return env;
		}

		std::string getPackageName() const
		{
			return getEnv("package");
		}

		void setEnv(const std::string& envName, const std::string& envData) const
		{
			IBuilder->setEnv(IBuilder->_this, envName.c_str(), envData.c_str());
		}

		void logInfo(const std::string& txt) const
		{
			return IBuilder->logInfo(IBuilder->_this, txt.c_str());
		}

		void logSuccess(const std::string& txt) const
		{
			return IBuilder->logSuccess(IBuilder->_this, txt.c_str());
		}

		void logError(const std::string& txt) const
		{
			return IBuilder->logError(IBuilder->_this, txt.c_str());
		}

		void logErrorThrow(const std::string& txt) const
		{
			return IBuilder->logErrorThrow(IBuilder->_this, txt.c_str());
		}

		void logWarning(const std::string& txt) const
		{
			return IBuilder->logWarning(IBuilder->_this, txt.c_str());
		}

		static void BUILDER_CALL _REPL_VAR_REFS(void* outCppStr, const char* outStr)
		{
			static_cast<std::string*>(outCppStr)->append(outStr);
		}

		std::string replVarRefs(const std::string& txt) const
		{
			std::string out;
			IBuilder->replVarRefs(IBuilder->_this, txt.c_str(), &out, _REPL_VAR_REFS);
			return out;
		}
	};

	class MCBuildContextView
	{
	private:
		const IMCBuildContext* IContext;
	public:
		MCBuildContextView(const IMCBuildContext* context) : IContext(context) {}

		MFSI::MemoryPathView getBuildPath()
		{
			return *IContext->buildPath;
		}

		MFSI::MemoryPathView getBehPath()
		{
			return *IContext->behFS;
		}

		MFSI::MemoryPathView getResPath()
		{
			return *IContext->resFS;
		}

		// 分配MOD目录 (将自动构造相关QuModLibs环境 可以直接操作modMain.py)
		MFSI::MemoryPathView getModPath()
		{
			return IContext->modFS(IContext->_this);
		}
	};
}
#endif